package ext.mango.aa.base;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

import mango.Mango;
import mango.functions.FuncServlet;
import mango.functions.FuncString;
import mango.service.ServiceManager;
import mango.web.Action;
import mango.web.Info;
import mango.web.InfoMaker;
import ext.mango.aa.eo.EOAAUser;
import ext.mango.aa.so.AppManager;
import ext.mango.base.Messages;
import ext.mango.functions.Func;
import ext.mango.utils.Logger2;

public class HttpFilter implements Filter {
    public void destroy() {
    }

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain)
            throws IOException, ServletException {
        
        HttpServletRequest request = (HttpServletRequest) req;
        request.setCharacterEncoding(Mango.getProperty(Mango.ENCODING));
        resp.setContentType("text/html; charset=" + Mango.getProperty(Mango.ENCODING)); //$NON-NLS-1$
        String an = FuncServlet.getRequestActionName(request);
        if (an != null) {
            an = an.substring(an.lastIndexOf(".") + 1); //$NON-NLS-1$
        }
        EOAAUser user = (EOAAUser) request.getSession().getAttribute(EOAAUser.class.getName());
        Logger2.setInfo(//request.getSession().getId(),
                       user == null ? "" : user.getId(),
                       request.getMethod(),
                       an,
                       request.getParameter(Action.REQUEST_METHOD),
                       ServiceManager.getService(AppManager.class).increaseRequest()
               );
        Mango.getLog().info("--REQUEST START--"); //$NON-NLS-1$
        long start = System.currentTimeMillis();
        //跨站访问校验
        String server = FuncServlet.getServer(request);
        String referer = request.getHeader("referer"); //$NON-NLS-1$
        if (FuncString.isBlank(referer) || !referer.startsWith(server)) {
            Mango.getLog().debug("referer:" + referer + " is not come from current site !"); //$NON-NLS-1$ //$NON-NLS-2$
            return;
        }
        
        //登陆校验
        if (request.getServletPath().endsWith("login.do") && "dl".equals(request.getParameter(Action.REQUEST_METHOD))) { //$NON-NLS-1$ //$NON-NLS-2$
        }
        else {
            if (user == null) {
                Info info = InfoMaker.getInstance().generateInfo(Info.TIMEOUT, Messages.getString(Func.getLanguage(request), "logintimeout")); //$NON-NLS-1$
                String ret = info.toJSON();
                Mango.getLog().info(ret);
                resp.getWriter().write(ret);
                return;
            }
            Logger2.setInfo(//request.getSession().getId(),
                           user.getId(),
                           request.getMethod(),
                           an,
                           request.getParameter(Action.REQUEST_METHOD),
                           ServiceManager.getService(AppManager.class).getRequestTimes()
                   );
        }
        chain.doFilter(req, resp);
        Mango.getLog().info("--REQUEST END|" + (System.currentTimeMillis() - start) + " ms"); //$NON-NLS-1$ //$NON-NLS-2$
        Logger2.removeInfo();
    }

    public void init(FilterConfig arg0) throws ServletException {
    }
}
